$(function(){
	var i =0;
	var n1 = 32;
	if(localStorage.getItem("Lottery_num")){
		var n2 = localStorage.getItem("Lottery_num");
		$(".num").html(n2);
	}else{
		localStorage.setItem("Lottery_num",3);
		var n2 = 3;//次数
	}
	var arr= [];
	var prize_lists = [];
	$.ajax({
		type:"get",
		url:"../js/prize.json",
		async:true,
		success:function(res){
			arr = res;
		}
	});
	$(".start").on("click",startLottery);
	function startLottery(){
		n2--;
		if(n2<0){
			n2=0;			
			$(".pop_up_Run_out").show();
		}else{
			$(".start").off();
			var a1 =Math.floor(Math.random()*8);
			n1 =32+(8-i)+a1;
			Lottery();
			//存奖品
			localStorage.setItem("Lottery_num",n2);	
			if(a1!=4){
				var d = new Date();
				var obj = {
					img:arr[a1].img,
					title:arr[a1].title,
					s1:"中奖时间："+d.getFullYear()+"年"+(d.getMonth()+1)+"月"+d.getDate()+"日"+d.getHours()+"时"+d.getMinutes()+"分",
					s2:arr[a1].time,
					address:arr[a1].address,
					tel:arr[a1].tel,
					expiry:0
				}
				prize_lists.push(obj);
				localStorage.setItem("prize_listss",JSON.stringify(prize_lists));
			}			
		}
		$(".num").html(n2);
	}
	function Lottery(){
		$(".start").siblings("img").removeClass("active");
		 i++;
		 if(i==8){
		 	i=0;
		 }
		 if(i==3){
		 	$(".sudoku img").eq(5).addClass("active");
		 }else if(i==4){
		 	$(".sudoku img").eq(8).addClass("active");
		 }else if(i==5){
		 	$(".sudoku img").eq(7).addClass("active");
		 }else if(i==6){
		 	$(".sudoku img").eq(6).addClass("active");
		 }else if(i==7){
		 	$(".sudoku img").eq(3).addClass("active");
		 }else{
		 	$(".sudoku img").eq(i).addClass("active");
		 }
		 n1--;
		 if(n1>10){
		 	setTimeout(Lottery,50)
		 }else if(n1>9){
		 	setTimeout(Lottery,100)
		 }else if(n1>8){
		 	setTimeout(Lottery,150)
		 }else if(n1>7){
		 	setTimeout(Lottery,200)
		 }else if(n1>6){
		 	setTimeout(Lottery,250)
		 }else if(n1>5){
		 	setTimeout(Lottery,300)
		 }else if(n1>4){
		 	setTimeout(Lottery,350)
		 }else if(n1>3){
		 	setTimeout(Lottery,400)
		 }else if(n1>2){
		 	setTimeout(Lottery,450)
		 }else if(n1>1){
		 	setTimeout(Lottery,500)
		 }else if(n1>0){
		 	setTimeout(Lottery,550);
		 }else{
		 	setTimeout(function(){
		 		$(".pop_up1").show();
			 	if(i==4){
			 		$(".pop_up_info1 h2").html("非常遗憾，没有中奖");
			 		$(".pop_up_info1 img").attr("src","../img/sudoku_09.jpg");
			 		$(".pop_up_info1 p").html("");
			 		$(".pop_up_info1 b").html("");
			 	}else{
			 		console.log(arr);
			 		$(".pop_up_info1 h2").html("恭喜你，中奖了");
			 		$(".pop_up_info1 img").attr("src",arr[i].img);
			 		$(".pop_up_info1 .prize_name").html(arr[i].title);
			 		$(".pop_up_info1 .price").html(arr[i].price);
			 		$(".pop_up_info1 .address").html(arr[i].address);
			 		$(".pop_up_info1 .tel").html(arr[i].tel);
			 	}
			 	$(".start").on("click",startLottery);
		 	},300)		 	
		 }
		 
	}
	$(".pop_up button").click(function(){
		$(".pop_up").hide();
	})
	$(".activity_rules").click(function(){
		$(".pop_up_rule").show();
	})
})
